package com.ll.mapdemo;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @author 刘龙
 * @version 1.0
 */
public class MapExercise {
    public static void main(String[] args) {
        Map hashMap = new HashMap();

        hashMap.put(1, new Employee(1, "ll1", 68000.0));
        hashMap.put(2,  new Employee(2, "ll2", 4000.0));
        hashMap.put(3,  new Employee(3, "ll3", 9000.0));

        // 使用keySet => 增强for
        Set keySet = hashMap.keySet();
        for (Object key : keySet) {
            // 先获取value
            Employee employee = (Employee) hashMap.get(key);
            if (employee.getSalary() > 18888) {
                System.out.println(key + "-" + employee);
            }
        }

        // 使用EntrySet -> 迭代器
        Set entrySet = hashMap.entrySet();
        Iterator iterator = entrySet.iterator();
        while (iterator.hasNext()) {
            Map.Entry entry = (Map.Entry)iterator.next();
            // 通过entry 取得key 和 value
            Employee employee = (Employee) entry.getValue();
            if (employee.getSalary() > 18888) {
                System.out.println(entry.getKey() + "-" + employee);
            }
        }
    }
}

class Employee {
    private Integer id;
    private String name;
    private Double salary;

    public Employee(Integer id, String name, Double salary) {
        this.id = id;
        this.name = name;
        this.salary = salary;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getSalary() {
        return salary;
    }

    public void setSalary(Double salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", salary=" + salary +
                '}';
    }
}
